﻿<%@ WebHandler Language="C#" Class="usercontact" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web.Script.Serialization;

using Newtonsoft.Json;
using VISS.Web.Frontend;
using NCT.Framework;
using VISS.API.Users;
using VISS.MailImpl;

public class usercontact : IHttpHandler, IRequiresSessionState
{
    string resp = string.Empty;
    string result = "";
    IUserContactRequest _iUserRequest = StructureMapFactory.GetInstance<IUserContactRequest>();

    public void ProcessRequest(HttpContext context)
    {
        string type = "";

        type = context.Request.Form["type"].ToString();

        switch (type)
        {
            case "update":
                UpdateContact(context);
                break;
            case "request":
                RequestContact(context);
                break;
            case "deny":
                Deny(context);
                break;
            case "allow":
                Allow(context);
                break;
            case "delete":
                Remove(context);
                break;
            case "sendpass":
                Remove(context);
                break;
            case "chatwith":
                string key = context.Request.Form["userkey"].ToString();
                chatWith(context,key);
                break;
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
        }

    private void chatWith(HttpContext context,string UserKey)
    {
        IUserGeneralProfile _objectGeneralProfile = StructureMapFactory.GetInstance<IUserGeneralProfile>();

        UserGeneralProfileEnt userGeneralProfileEnt = _objectGeneralProfile.GetByUserKey(UserKey);

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : userGeneralProfileEnt.Name));

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();

    }
    public void Remove(HttpContext context)
    {
        string ReceiverUserKey = string.Empty;
        string SenderUserKey = string.Empty;

        ReceiverUserKey = context.Request.Form["ReceiverUserKey"].ToString();
        SenderUserKey = context.Request.Form["SenderUserKey"].ToString();

        if (!_iUserRequest.RemoveUserFromAllowedList(ReceiverUserKey, SenderUserKey))
        {
            result = "0";
        }

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : (result == "0" ? "failed" : "success")));

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();

    }

    public void Allow(HttpContext context)
    {
        string ReceiverUserKey = string.Empty;
        string SenderUserKey = string.Empty;

        ReceiverUserKey = context.Request.Form["ReceiverUserKey"].ToString();
        SenderUserKey = context.Request.Form["SenderUserKey"].ToString();

        if (!_iUserRequest.AllowRequest(ReceiverUserKey, SenderUserKey))
        {
            result = "0";
        }

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : (result == "0" ? "failed" : "success")));

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();

    }

    public void Deny(HttpContext context)
    {
        string ReceiverUserKey = string.Empty;
        string SenderUserKey = string.Empty;

        ReceiverUserKey = context.Request.Form["ReceiverUserKey"].ToString();
        SenderUserKey = context.Request.Form["SenderUserKey"].ToString();

        if (!_iUserRequest.DenyRequest(SenderUserKey, ReceiverUserKey))
        {
            result = "0";
        }

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : (result == "0" ? "failed" : "success")));

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();

    }
    public void RequestContact(HttpContext context)
    {
        string ReceiverUserKey = string.Empty;
        string SenderUserKey = string.Empty;

        ReceiverUserKey = context.Request.Form["ReceiverUserKey"].ToString();
        SenderUserKey = context.Request.Form["SenderUserKey"].ToString();


        if (!_iUserRequest.SendViewContacInfoRequest(SenderUserKey, ReceiverUserKey))
        {
            result = "0";
        }

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : (result == "0" ? "failed" : "success")));

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();

    }


    public void UpdateContact(HttpContext context)
    {
        UserContext userContext = LoginFactory.GetCurrentUser();

        if (userContext == null)
        {
            result = "-1";
        }
        string email = string.Empty;
        string homePhone = string.Empty;
        string mobile = string.Empty;
        string address = string.Empty;

        email = context.Request.Form["email"].ToString();

        homePhone = context.Request.Form["homephone"].ToString();

        mobile = context.Request.Form["mobile"].ToString();

        address = context.Request.Form["address"].ToString();

        //update contact

        IUserContactInfo _objectContact = StructureMapFactory.GetInstance<IUserContactInfo>();
        UserContactInfoEnt contactObject = _objectContact.GetByUserKey(userContext.UserKey);
        contactObject.Email = email.Trim();
        contactObject.HomePhone = homePhone.Trim();
        contactObject.MobiPhone = mobile.Trim();
        contactObject.Address = address.Trim();
        contactObject.UserKey = userContext.UserKey;

        if (!_objectContact.Update(contactObject))
        {
            result = "0";
        }

        Hashtable htbl = new Hashtable();

        htbl.Add("status", (result == "-1" ? "error" : (result == "0" ? "failed" : "success")));

        htbl.Add("key", result);

        resp = GetJSONStringFromObject(htbl);

        context.Response.Clear();

        context.Response.Write(resp);

        context.Response.End();
    }

    public static void WriteResponseJson(Hashtable htbl)
    {

    }

    public static string GetJSONStringFromObject<T>(T obj)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(obj);
    }

    public static T GetJSONObjectFromString<T>(string value)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Deserialize<T>(value);
    }
}

